package com.porting;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

public class TemplateUtils {

    public static String generateTemplate(String ftl, Map<String, Object> params){
        try {
            Configuration configuration = new Configuration();
            configuration.setClassForTemplateLoading(TemplateUtils.class, "/");
            configuration.setDefaultEncoding("UTF-8");
            configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
            Template template = configuration.getTemplate(ftl);
            Writer stringWriter = new StringWriter();
            template.process(params, stringWriter);
            return stringWriter.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
